package DRC;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import DRCOMM.DRHTTP_API;
import DRCOMM.DR_SESSION;
import DRCOMM.FJW_URI;
import DRCOMM.record_data_set;

public class protocol {

    public int protocol_id=-1;
    public String protocol="";
    public String memo="";
    
    public DRCDB drcdb;

    public protocol(DRCDB drcdb){
        this.drcdb=drcdb;
    }

    public protocol(String protocol,String memo,String description){
        this.protocol=protocol;
        this.memo=memo;
    }

    public protocol(int protocol_id,String protocol,String memo,String description){
        this.protocol_id=protocol_id;
        this.protocol=protocol;
        this.memo=memo;
    }

    public void set_protocol_id(int protocol_id){
        this.protocol_id=protocol_id;
    }

    public record_data_set read_protocols(){
        DRCDB db=this.drcdb;
        String sqlstr="select * from protocol";
        record_data_set rds=db.executeQuerySQL(sqlstr);
        return rds;
    }

    /**
     * 关联WEB API列表
     * @param api_lists
     * @param fpsdb
     * @param drcdb
     */
    public static void associate_actions(List<DRHTTP_API> api_lists,DRCDB drcdb) throws ClassNotFoundException{
        @SuppressWarnings("unchecked")
        Class<?> CLS=(Class<protocol>) Class.forName("DRC.protocol");
        System.out.println("CLASS: "+CLS.getClass().getName());
        for(Method method:CLS.getDeclaredMethods()){
            FJW_URI fjw_uri=method.getAnnotation(FJW_URI.class);
            System.out.println(method.toString());
            if(fjw_uri!=null){
                System.out.println(method.toString());
                System.out.println(fjw_uri.value());
                try {
                    api_lists.add((DRHTTP_API)method.invoke(CLS, drcdb, fjw_uri.value()));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @FJW_URI(value ="protocol/protocoList")
    public static DRHTTP_API protocoList(DRCDB drcdb,String URI){
        return new DRHTTP_API(drcdb,URI){
            @Override
            public String getResult(Map<String, Object> parameters,DR_SESSION drs){
                String content="";
                DRCDB db=drcdb;
                String sqlstr="select protocol_id,protocol from protocol where type=0";
                record_data_set rds=db.executeQuerySQL(sqlstr);
                content=record_data_set.get_answer_json_str(0,"",rds);
                return content;
            }
        };
    }

    @FJW_URI(value ="protocol/protocoList4collect")
    public static DRHTTP_API protocoList4collect(DRCDB drcdb,String URI){
        return new DRHTTP_API(drcdb,URI){
            @Override
            public String getResult(Map<String, Object> parameters,DR_SESSION drs){
                String content="";
                DRCDB db=drcdb;
                String sqlstr="select protocol_id,protocol from protocol where type=1";
                record_data_set rds=db.executeQuerySQL(sqlstr);
                content=record_data_set.get_answer_json_str(0,"",rds);
                return content;
            }
        };
    }

    public static void main(String[] args){
        DRCDB drcdb=new DRCDB();
        protocol hs=new protocol(drcdb);
        record_data_set rds=hs.read_protocols();
        // for(Map<String, String> row:rds.datas){
        //     System.out.println(row.get("protocol"));
        // }
        System.out.println(record_data_set.get_answer_json_str(0,"",rds));
    }
}